﻿using System;
using System.Data;
using System.Collections.Generic;
using Catering_Model;
using Catering_DBOpertion;
namespace Catering_Rules
{
    /// <summary>
    /// DishNutrien
    /// </summary>
    public partial class DishNutrien
    {
        private readonly Catering_Operation.DishNutrien dal = new Catering_Operation.DishNutrien();
        public DishNutrien()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Catering_Model.DishNutrien model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.DishNutrien model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            return dal.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            return dal.DeleteList(idlist);
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteListBywhere(string where)
        {
            return dal.DeleteListBywhere(where);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.DishNutrien GetModel(int id)
        {

            return dal.GetModel(id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Catering_Model.DishNutrien GetModelByCache(int id)
        {

            string CacheKey = "DishNutrienModel-" + id;
            object objModel = Catering_DBOpertion.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(id);
                    if (objModel != null)
                    {
                        int ModelCache = Catering_DBOpertion.ConfigHelper.GetConfigInt("ModelCache");
                        Catering_DBOpertion.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Catering_Model.DishNutrien)objModel;
        }

        #region 新写的
        List<string> sql = new List<string>();
        public void TransAdd(List<Catering_Model.DishNutrien> model)
        {
            for (int i = 0; i < model.Count; i++)
            {
                string insert = string.Format("insert into DishNutrien(Dishid,Content,element_name) values('{0}','{1}','{2}')", model[i].Dishid, model[i].Content, model[i].element_name);
                sql.Add(insert);
            }
        }
        public void ExecTrans()
        {
            if (sql != null)
            {
                DbHelperSQL.TransQuery(sql);
                sql.Clear();
            }
        }
        #endregion

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.DishNutrien> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.DishNutrien> DataTableToList(DataTable dt)
        {
            List<Catering_Model.DishNutrien> modelList = new List<Catering_Model.DishNutrien>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Catering_Model.DishNutrien model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Catering_Model.DishNutrien();
                    if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "")
                    {
                        model.id = int.Parse(dt.Rows[n]["id"].ToString());
                    }
                    if (dt.Rows[n]["Dishid"] != null && dt.Rows[n]["Dishid"].ToString() != "")
                    {
                        model.Dishid = int.Parse(dt.Rows[n]["Dishid"].ToString());
                    }
                    if (dt.Rows[n]["element_name"] != null && dt.Rows[n]["element_name"].ToString() != "")
                    {
                        model.element_name = dt.Rows[n]["element_name"].ToString();
                    }
                    if (dt.Rows[n]["Content"] != null && dt.Rows[n]["Content"].ToString() != "")
                    {
                        model.Content = dt.Rows[n]["Content"].ToString();
                    }
                    if (dt.Rows[n]["fatherid"] != null && dt.Rows[n]["fatherid"].ToString() != "")
                    {
                        model.fatherid = dt.Rows[n]["fatherid"].ToString();
                    }
                    if (dt.Rows[n]["temp1"] != null && dt.Rows[n]["temp1"].ToString() != "")
                    {
                        model.temp1 = dt.Rows[n]["temp1"].ToString();
                    }
                    if (dt.Rows[n]["temp2"] != null && dt.Rows[n]["temp2"].ToString() != "")
                    {
                        model.temp2 = dt.Rows[n]["temp2"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 通过菜ID集合获取元素含量总和
        /// </summary>
        public DataSet GetDishNutrienSumByid(string idlist)
        {
            return dal.GetDishNutrienSumByid(idlist);
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method
    }
}